package com.cxm.qyyz.utils;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.WindowManager;

import androidx.core.content.ContextCompat;

import com.cxm.qyyz.R;
import com.cxm.qyyz.app.App;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * NursingWorker
 * Created by downtail on 2020/9/3
 */
public class AppUtil {

    private final Stack<WeakReference<Activity>> mActivityStack;

    private AppUtil() {
        mActivityStack = new Stack<>();
    }

    private static class Inner {
        static final AppUtil mInstance = new AppUtil();
    }

    public static AppUtil getInstance() {
        return Inner.mInstance;
    }

    public void push(Activity activity) {
        mActivityStack.push(new WeakReference<>(activity));
    }

    public void pop(Activity target) {
        for (Iterator<WeakReference<Activity>> it = mActivityStack.iterator(); it.hasNext(); ) {
            WeakReference<Activity> mRef = it.next();
            Activity activity = mRef.get();
            if (target == activity && activity != null) {
                activity.finish();
                mActivityStack.remove(mRef);
                return;
            }
        }
    }

    public Activity peek() {
        if (!mActivityStack.isEmpty()) {
            WeakReference<Activity> mRef = mActivityStack.peek();
            return mRef.get();
        }
        return null;
    }

    public void finishAllActivity() {
        for (Iterator<WeakReference<Activity>> it = mActivityStack.iterator(); it.hasNext(); ) {
            WeakReference<Activity> mRef = it.next();
            Activity activity = mRef.get();
            if (activity != null) {
                activity.finish();
            }
        }
        mActivityStack.clear();
    }

    public void exitApp() {
        try {
            finishAllActivity();
            // 退出JVM(java虚拟机),释放所占内存资源,0表示正常退出(非0的都为异常退出)
            System.exit(0);
            // 从操作系统中结束掉当前程序的进程
            android.os.Process.killProcess(android.os.Process.myPid());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 判断某activity是否处于栈顶
     *
     * @return true在栈顶 false不在栈顶
     */
    public static boolean isActivityTop(Class cls, Context context) {
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        String name = manager.getRunningTasks(1).get(0).topActivity.getClassName();
        return name.equals(cls.getName());
    }


    /**
     * 获取html中的所有图片
     * @param compatText
     * @return
     */
    public static List<String> filterImages(String compatText){


        List<String> list = new ArrayList<>();
        String img;
        Pattern p_image;
        Matcher m_image;
        // String regEx_img = "<img.*src=(.*?)[^>]*?>"; //图片链接地址
        String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
        p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
        m_image = p_image.matcher(compatText);
        while (m_image.find()) {
            // 得到<img />数据
            img = m_image.group();
            // 匹配<img>中的src数据
            Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
            while (m.find()) {
                list.add(m.group(1));
            }
        }
        return list;


    }
    public void showToast(String message){
//        ToastUtils.getDefaultMaker()
//                .setTextColor(ContextCompat.getColor(App.getInstance(), R.color.color_ffffff))
//                .setGravity(Gravity.CENTER, 0, 0)
//                .setBgColor(ContextCompat.getColor(App.getInstance(), R.color.color_000000))
//                .show(message);
    }
    public void showToast(int message){
//        ToastUtils.getDefaultMaker()
//                .setTextColor(ContextCompat.getColor(App.getInstance(), R.color.color_ffffff))
//                .setGravity(Gravity.CENTER, 0, 0)
//                .setBgColor(ContextCompat.getColor(App.getInstance(), R.color.color_000000))
//                .show(message);
    }


    public static int dp2px(Context context, float dipValue) {
        try {
            final float scale = context.getResources().getDisplayMetrics().density;
            return (int) (dipValue * scale + 0.5f);
        } catch (Exception e) {
            return (int) dipValue;
        }
    }

    public static int px2dp(Context context, float px) {
        try {
            final float scale = context.getResources().getDisplayMetrics().density;
            return (int) (px / scale + 0.5f);
        } catch (Exception e) {
            return (int) px;
        }
    }

    public static int getPhoneWidthPixels(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics var2 = new DisplayMetrics();
        if (wm != null) {
            wm.getDefaultDisplay().getMetrics(var2);
        }

        return var2.widthPixels;
    }

    public static int getPhoneHeightPixels(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics var2 = new DisplayMetrics();
        if (wm != null) {
            wm.getDefaultDisplay().getMetrics(var2);
        }

        return var2.heightPixels;
    }

    public static boolean isWeixinAvilible(Context context) {
        final PackageManager packageManager = context.getPackageManager();// 获取packagemanager
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals("com.tencent.mm")) {
                    return true;
                }
            }
        }

        return false;
    }
}
